﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Threading;

using CRM.Entity.Extra;
using CRM.Entity;
using CRM.Dictionary.Box;
using CRM.Entity.Page;


namespace CRM.Dictionary.Service
{
    public partial class News_Type_DicService
    {
        public static ReaderWriterLockSlim News_Type_rws = new ReaderWriterLockSlim();

        public News_TypePage News_Type_List()
        {
            try
            {
                News_Type_rws.EnterReadLock();

                IEnumerable<News_Type> listEntity = DictionaryBox.News_TypeDictionary.Values;

                var News_TypePage = new News_TypePage()
                {
                    ListEntity = listEntity.ToList(),
                    RecordCount = listEntity.Count()
                };

                return News_TypePage;
            }
            finally
            {
                News_Type_rws.ExitReadLock();
            }
        }

        public News_TypePage News_Type_List(PageInfo page)
        {
            try
            {
                News_Type_rws.EnterReadLock();

                IEnumerable<News_Type> listEntity = DictionaryBox.News_TypeDictionary.Values;

                var News_TypePage = new News_TypePage()
                {
                    ListEntity = listEntity.AsParallel().OrderByDescending(i => i.type_id)
                                  .Skip((page.PageIndex - 1) * page.PageSize).Take(page.PageSize).ToList(),
                    RecordCount = listEntity.AsParallel().Count()
                };

                return News_TypePage;
            }
            finally
            {
                News_Type_rws.ExitReadLock();
            }
        }
        
        public News_TypePage News_Type_List(Func<News_Type, bool> func)
        {
            try
            {
                News_Type_rws.EnterReadLock();

                IEnumerable<News_Type> listEntity = DictionaryBox.News_TypeDictionary.Values;

                var News_TypePage = new News_TypePage()
                {
                    ListEntity = listEntity.Where(func).ToList(),
                    RecordCount = listEntity.Count()
                };

                return News_TypePage;
            }
            finally
            {
                News_Type_rws.ExitReadLock();
            }
        }

        public News_TypePage News_Type_List(PageInfo page, Func<News_Type, bool> func)
        {
            try
            {
                News_Type_rws.EnterReadLock();

                IEnumerable<News_Type> listEntity = DictionaryBox.News_TypeDictionary.Values
                                                                .Where(func);

                var News_TypePage = new News_TypePage()
                {
                    ListEntity = listEntity.AsParallel().OrderByDescending(i => i.type_id)
                                  .Skip((page.PageIndex - 1) * page.PageSize).Take(page.PageSize).ToList(),
                    RecordCount = listEntity.AsParallel().Count()
                };

                return News_TypePage;
            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                News_Type_rws.ExitReadLock();
            }
        }

        public List<News_Type> News_Type_List_Top(int top, Func<News_Type, bool> func)
        {
            try
            {
                News_Type_rws.EnterReadLock();

                IEnumerable<News_Type> listEntity = DictionaryBox.News_TypeDictionary.Values.Where(func);

                return listEntity.AsParallel().OrderByDescending(i => i.type_id).Take(top).ToList();
            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                News_Type_rws.ExitReadLock();
            }
        }

        public News_Type News_Type_Multiple(Func<News_Type, bool> func)
        {
            try
            {
                News_Type_rws.EnterReadLock();

                return DictionaryBox.News_TypeDictionary.Values.Where(func).FirstOrDefault();
            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                News_Type_rws.ExitReadLock();
            }
        }

        public News_Type News_Type_Single(Int32 News_TypeID)
        {
            if(DictionaryBox.News_TypeDictionary.ContainsKey(News_TypeID))
                return DictionaryBox.News_TypeDictionary[News_TypeID];
            return null;
        }

        public bool News_Type_Add(News_Type News_Type)
        {
            try
            {
                News_Type_rws.EnterWriteLock();

                bool isSuccess = false;

                isSuccess = DictionaryBox.News_TypeDictionary.TryAdd(News_Type.type_id, News_Type);

                return isSuccess;
            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                News_Type_rws.ExitWriteLock();
            }
        }
        
        public bool News_Type_AddBatch(List<News_Type> list)
        {
            try
            {
                News_Type_rws.EnterWriteLock();

                bool isSuccess = false;

                if (list.Count == 0)
                    isSuccess = true;

                foreach (var item in list)
                    isSuccess = DictionaryBox.News_TypeDictionary.TryAdd(item.type_id, item);

                return isSuccess;
            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                News_Type_rws.ExitWriteLock();
            }
        }

        public bool News_Type_Update(News_Type News_Type)
        {
            try
            {
                News_Type_rws.EnterWriteLock();

                bool isSuccess = false;

                isSuccess = DictionaryBox.News_TypeDictionary.TryUpdate(News_Type.type_id,
                                          News_Type, DictionaryBox.News_TypeDictionary[News_Type.type_id]);

                return isSuccess;
            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                News_Type_rws.ExitWriteLock();
            }
        }
        
        public bool News_Type_UpdateBatch(List<News_Type> list)
        {
            try
            {
                News_Type_rws.EnterWriteLock();

                bool isSuccess = false;

                if (list.Count == 0)
                   isSuccess = true;

                foreach (var single in list)
                {
                    isSuccess = DictionaryBox.News_TypeDictionary.TryUpdate(single.type_id,
                                              single, DictionaryBox.News_TypeDictionary[single.type_id]);
                }

                return isSuccess;
            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                News_Type_rws.ExitWriteLock();
            }
        }

        public bool News_Type_Remove(News_Type News_Type)
        {
            try
            {
                News_Type_rws.EnterWriteLock();

                bool isSuccess = false;

                isSuccess = DictionaryBox.News_TypeDictionary.TryRemove(News_Type.type_id, out News_Type);

                return isSuccess;
            }
            finally
            {
                News_Type_rws.ExitWriteLock();
            }
        }
        
        public bool News_Type_RemoveList(List<Int64> list)
        {
            try
            {
                News_Type_rws.EnterWriteLock();

                bool isSuccess = false;

                if (list.Count == 0)
                    isSuccess = true;

                foreach (var id in list)
                {
                    News_Type single;

                    isSuccess = DictionaryBox.News_TypeDictionary.TryRemove(id, out single);
                }

                return isSuccess;
            }
            finally
            {
                News_Type_rws.ExitWriteLock();
            }
        }
    }
}
